package com.lbmb.mdps.workbench.service.impl;

import com.lbmb.mdps.commons.domain.MedicineReceive;
import com.lbmb.mdps.commons.exception.AuditException;
import com.lbmb.mdps.commons.exception.InventoryException;
import com.lbmb.mdps.commons.exception.MedicineException;
import com.lbmb.mdps.commons.exception.PurchaseException;
import com.lbmb.mdps.commons.utils.DateTimeUtil;
import com.lbmb.mdps.commons.utils.SnowFlakeIdUtil;
import com.lbmb.mdps.workbench.dao.*;
import com.lbmb.mdps.workbench.domain.Audit;
import com.lbmb.mdps.workbench.domain.Inventory;
import com.lbmb.mdps.workbench.domain.Medicine;
import com.lbmb.mdps.workbench.domain.Purchase;
import com.lbmb.mdps.workbench.service.MedicineService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 林文塨
 * @date 2022-05-30 23:12
 * description
 */
@Service("medicineService")
public class MedicineServiceImpl implements MedicineService {

    @Resource
    private MedicineDao medicineDao;
    @Resource
    private MedicineTypeDao medicineTypeDao;
    @Resource
    private AuditDao auditDao;
    @Resource
    private PurchaseDao purchaseDao;
    @Resource
    private InventoryDao inventoryDao;

    @Override
    public List<Medicine> medicinePageList(MedicineReceive medicineReceive) throws MedicineException {
        /*
         * 分页获取药品表功能
         * @author 林文塨
         * @date 2022/5/30 10:24
         * @param [employeeReceive]
         * @return java.util.List<com.lbmb.mdps.settings.domain.Employee>
         */

        List<Medicine> medicineList = medicineDao.medicinePageList(medicineReceive);

        if (medicineList == null) {
            throw new MedicineException("分页获取药品列表失败！");
        }

        for (Medicine medicine : medicineList) {
            medicine.setMedicineTypeVo(medicineTypeDao.getMedicineTypeById(medicine.getMedicineType()));
        }

        return medicineList;
    }

    @Override
    public Integer getMedicineTotal(MedicineReceive medicineReceive) throws MedicineException {
        /*
         * 获取药品分页总条数功能
         * @author 林文塨
         * @date 2022/5/30 10:42
         * @param [employeeReceive]
         * @return java.lang.Integer
         */
        Integer total = medicineDao.getMedicineTotal(medicineReceive);

        if (total < 0) {
            throw new MedicineException("获取药品分页总条数失败！");
        }

        return total;
    }

    @Override
    public void addMedicine(Medicine medicine) throws MedicineException {
        /*
         * 添加药品
         * @author 林文塨
         * @date 2022/5/30 13:06
         * @param [employee]
         * @return void
         */
        int result = medicineDao.addMedicine(medicine);

        if (result != 1) {
            throw new MedicineException("添加药品失败！");
        }
    }

    @Override
    public void editMedicine(Medicine medicine) throws MedicineException {
        /*
         * 修改药品
         * @author 林文塨
         * @date 2022/5/30 13:06
         * @param [employee]
         * @return void
         */
        int result = medicineDao.editMedicine(medicine);

        if (result != 1) {
            throw new MedicineException("修改药品失败！");
        }
    }

    @Override
    public void deleteMedicine(String[] deleteMedicineIds) throws MedicineException {
        /*
         * 删除药品
         * @author 林文塨
         * @date 2022/5/30 16:32
         * @param [deleteEmployeeIds]
         * @return void
         */
        int result = medicineDao.deleteMedicine(deleteMedicineIds);

        if (result <= 0) {
            throw new MedicineException("删除药品失败");
        }
    }

    @Override
    @Transactional
    public void purchaseMedicine(Purchase purchase) throws PurchaseException, AuditException, InventoryException {
        /*
         * 采购药品
         * @author 林文塨
         * @date 2022/6/1 8:46
         * @param [purchase]
         * @return void
         */

        /*添加采购订单*/
        purchase.setOrderTime(DateTimeUtil.getSysTime());
        purchase.setOrderBy(purchase.getCurrentUserId());
        purchase.setDeleteFlag("0");
        purchase.setId(SnowFlakeIdUtil.generateId());
        purchase.setState("待审核");
        int result1 = purchaseDao.addPurchase(purchase);

        if (result1 != 1) {
            throw new PurchaseException("添加采购订单失败！");
        }

        /*添加库存或者新建库存*/
        Inventory inventory = inventoryDao.getInventoryByCondition(purchase);

        /*如果库存中没有该订单，则新建一个库存*/
        if (inventory == null) {

            Inventory inventoryAdd = new Inventory();
            inventoryAdd.setId(SnowFlakeIdUtil.generateId());
            inventoryAdd.setMedicine(purchase.getMedicine());
            inventoryAdd.setSupplier(purchase.getSupplier());
            inventoryAdd.setWarehouse(purchase.getWarehouse());
            inventoryAdd.setPurchaseNumber(purchase.getNumber());
            inventoryAdd.setDeliverNumber(0);
            inventoryAdd.setExistedNumber(purchase.getNumber());

            int result2 = inventoryDao.addInventory(inventoryAdd);

            if (result2 != 1) {
                throw new InventoryException("添加库存记录失败！");
            }

            /*添加审核记录*/
            Audit audit = new Audit();
            audit.setId(SnowFlakeIdUtil.generateId());
            audit.setState("待审核");
            audit.setAuditOrder(purchase.getId());
            audit.setAuditBy(purchase.getOrderBy());
            audit.setAuditTime(DateTimeUtil.getSysTime());
            int result3 = auditDao.addAudit(audit);

            if (result3 != 1) {
                throw new AuditException("添加审核记录失败！");
            }
        } else {
            /*如果库存中有该订单，则更新该订单*/
            inventory.setPurchaseNumber(inventory.getPurchaseNumber()+purchase.getNumber());
            inventory.setExistedNumber(inventory.getExistedNumber()+purchase.getNumber());
            int result3 = inventoryDao.updateInventory(inventory);

            if (result3 != 1) {
                throw new InventoryException("更新库存失败！");
            }
        }


    }
}
